<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车头号码" prop="carHeadNum">
        <el-input
          v-model="queryParams.carHeadNum"
          placeholder="请输入车头号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="确认人" prop="confirmPerson">
        <el-input
          v-model="queryParams.confirmPerson"
          placeholder="请输入进站确认人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="确认时间" prop="confirmTime">
        <el-date-picker clearable
          v-model="queryParams.confirmTime"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="请选择确认开始时间">
        </el-date-picker>
        <el-date-picker clearable
          v-model="queryParams.createTime"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="请选择确认结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="进站站点" prop="stationId">
        <el-select
          v-model="queryParams.stationId"
          placeholder="进站站点"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="station in stationList"
            :key="station.stationName"
            :value="station.stationId"
            :label="station.stationName"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['gas:get_in:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['gas:get_in:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['gas:get_in:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['gas:get_in:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="get_inList" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="进站记录ID" align="center" prop="getInId" v-if="false" />
<!--      <el-table-column label="车头号码" align="center" prop="carHeadNum" />-->
      <el-table-column label="车身号码" align="center" prop="carBodyNum" />
      <el-table-column label="进站确认人" align="center" prop="confirmPerson" />
      <el-table-column label="进站确认时间" align="center" prop="confirmTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.confirmTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="进站ID" align="center" prop="stationId" v-if="false" />
      <el-table-column label="进站名" align="center" prop="stationName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['gas:get_in:edit']"-->
<!--          >修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['gas:get_in:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改进站记录
对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="进站车辆车头号" prop="carHeadNum">
          <el-select v-model="form.carHeadNum" placeholder="请选择车头号" @change="selectCar">
            <el-option
              v-for="car in carInfoList"
              :key="car.carHeadNum"
              :value="car.carCode"
              :label="car.carHeadNum"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车身号" prop="carBodyNum">
          <el-input v-model="form.carBodyNum" placeholder="请输入车身号" readonly />
        </el-form-item>
        <el-form-item label="进站名" prop="stationId">
          <el-select v-model="form.stationId" placeholder="请选择预约气站" @change="selectStation">
            <el-option
              v-for="station in stationList"
              :key="station.stationName"
              :value="station.stationId"
              :label="station.stationName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="进站确认人" prop="confirmPerson">
          <el-select v-model="form.confirmPerson" placeholder="请选择进站确认人" @change="selectRegister">
            <el-option
              v-for="register in userList"
              :key="register.nickName"
              :value="register.nickName"
              :label="register.nickName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="进站时间" prop="confirmTime">
          <el-date-picker clearable
            v-model="form.confirmTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择进站确认时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGet_in, getGet_in, delGet_in, addGet_in, updateGet_in } from "@/api/gas/get_in";
import { listStation } from "@/api/base/station";
import { getRecentCarInfoList } from "@/api/gas/register"
import { getUserProfile } from "@/api/system/user";
import { allocatedUserList } from "@/api/system/role";
import format from '@/plugins/format.js'

export default {
  name: "Get_in",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      get_inList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        carHeadNum: null,
        confirmPerson: null,
        confirmTime: null,
        stationId: null,
        stationName: null,
      },
      // 表单参数
      form: {},
      stationList: [],
      stationInfoList_s: {},
      carInfoList: [],
      carInfoList_s: {},
      userList: [],
      user: {
        nickName: "",
        phonenumber: ""
      },
      stationList: [],
      // 表单校验
      rules: {
        carHeadNum: [
          { required: true, message: "进站车辆不能为空", trigger: "blur" }
        ],
        stationId: [
          { required: true, message: "进站名不能为空", trigger: "blur" }
        ],
        confirmPerson: [
          { required: true, message: "进站确认人不能为空", trigger: "blur" }
        ],
        confirmTime: [
          { required: true, message: "进站确认时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询进站记录
列表 */
    getList() {
      this.loading = true;
      listGet_in(this.queryParams).then(response => {
        this.get_inList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      listStation().then(response => {
        this.stationList = response.rows;
        var data_s = {};
        response.rows.forEach(function(row, index) {
          var stationInfo = {
            text: row.stationName,
            value: row.stationId
          }
          data_s[row.stationId] = row.stationName
        })
        this.stationInfoList_s = data_s
      });
      var params = {}, now = new Date();
      params.registerTime = format(now, 'YYYY-MM-DD HH:mm:ss')
      now.setDate(now.getDate() - 5);
      params.createTime = format(now, 'YYYY-MM-DD HH:mm:ss')
      params.status = '0'
      getRecentCarInfoList(params).then(response => {
        this.carInfoList = response;
        var data_s = {};
        response.forEach(function(row, index) {
          var carInfo = {
            text: row.carHeadNum,
            value: row.carCode
          }
          data_s[row.carCode] = row
        })
        this.carInfoList_s = data_s
      });
      getUserProfile().then(response => {
        this.user = response.data
      })
      allocatedUserList({roleId: 4}).then(response => {
        this.userList = response.rows
      })
    },
    selectStation(value) {
      this.form.stationName = this.stationInfoList_s[value]
    },
    selectCar(value) {
      if (this.carInfoList_s[value]) {
        this.form.carHeadNum = this.carInfoList_s[value].carHeadNum;
        this.form.carBodyNum = this.carInfoList_s[value].carBodyNum;
        this.form.carCode = this.carInfoList_s[value].carCode;
        this.form.registerId = this.carInfoList_s[value].registerId;
      }
    },
    selectRegister() {

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        getInId: null,
        carHeadNum: null,
        confirmPerson: this.user.nickName,
        confirmTime: new Date(),
        stationId: this.stationList[0].stationId,
        stationName: this.stationList[0].stationName,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.getInId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const getInId = row.getInId || this.ids
      getGet_in(getInId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.getInId != null) {
            updateGet_in(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.confirmTime = format( this.form.confirmTime, 'YYYY-MM-DD HH:mm:ss')
            addGet_in(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const getInIds = row.getInId || this.ids;
      this.$modal.confirm('是否确认删除编号为"' + getInIds + '"的数据项？').then(function() {
        return delGet_in(getInIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('gas/get_in/export', {
        ...this.queryParams
      }, `get_in_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
